package com.example.himalaya.presenters;

import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.view.callback.IDownloadedMusicDaoCallBack;
import com.example.himalaya.view.callback.IDownloadedMusicViewCallback;
import com.example.himalaya.model.dao.DownloadedMusicDao;
import com.example.himalaya.presenters.interfaces.IDownloadMusicPresenter;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class DownloadedMusicPresenter implements IDownloadMusicPresenter {

    // Logic Object
    private DownloadedMusicDao mDownloadedMusicDao = new DownloadedMusicDao();

    // Callback
    private List<IDownloadedMusicViewCallback> mCallbacks = new ArrayList<>();

    private static DownloadedMusicPresenter mDownloadedMusicPresenter;

    public static DownloadedMusicPresenter getDownloadedMusicPresenter() {

        if (mDownloadedMusicPresenter == null) {
            synchronized (DownloadedMusicPresenter.class) {
                if (mDownloadedMusicPresenter == null) {
                    mDownloadedMusicPresenter = new DownloadedMusicPresenter();
                }
            }
        }
        return mDownloadedMusicPresenter;
    }

    private DownloadedMusicPresenter() {
        initCallBack();
    }


    @Override
    public void getMusic() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                mDownloadedMusicDao.getMusic();
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void deleteMusic() {

    }

    @Override
    public void searchMusic() {

    }


    private void initCallBack() {
        mDownloadedMusicDao.registerCallback(new IDownloadedMusicDaoCallBack() {
            @Override
            public void onMusicLoaded(final List<Track> tracks) {

                // 通知UI更新数据
                // 从IO 线程到 主线程
                BaseApplication.getsHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        for (IDownloadedMusicViewCallback callback : mCallbacks) {
                            callback.onMusicLoaded(tracks);
                        }
                    }
                });
            }

            @Override
            public void onMusicDeleted(boolean isOk) {

            }

            @Override
            public void onMusicSearched(List<Track> tracks) {

            }
        });
    }

    @Override
    public void registerViewCallback(IDownloadedMusicViewCallback iDownloadedMusicViewCallback) {
        if (mCallbacks != null) {
            if (!mCallbacks.contains(iDownloadedMusicViewCallback)) {
                mCallbacks.add(iDownloadedMusicViewCallback);
            }
        }
    }

    @Override
    public void unRegisterViewCallback(IDownloadedMusicViewCallback iDownloadedMusicViewCallback) {
        if (mCallbacks != null) {
            mCallbacks.remove(iDownloadedMusicViewCallback);
        }
    }
}
